package com.example.video_player;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.io.File;

import android.Manifest;
import android.app.DownloadManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.L;

public class MainActivity extends AppCompatActivity {
    private Button Sql;
    ImageButton over;
    ListView listview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview = (ListView) findViewById(R.id.list);
        Sql = (Button) findViewById(R.id.button);
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
        ;
        Sql.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String filePath = ((EditText) findViewById(R.id.editText)).getText().toString();
                final File file = new File(filePath);
                final File[] files = file.listFiles(new MyFilter());
                if (files == null) {
                    Toast toast = Toast.makeText(MainActivity.this, "空目录！", Toast.LENGTH_SHORT);
                    toast.show();
                } else {
                    final String[] s = new String[files.length];
                    for (int i = 0; i < files.length; i++) {
                        s[i] = files[i].getName();
                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list, s);
                    listview.setAdapter(adapter);
                    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            //Toast.makeText(MainActivity.this, files[position].getAbsolutePath(), Toast.LENGTH_SHORT).show();
                            Bundle data = new Bundle();
                            data.putSerializable("path", files[position].getAbsolutePath());
                            Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                            intent.putExtras(data);
                            startActivity(intent);
                        }
                    });
                }
            }
        });
        over = (ImageButton) findViewById(R.id.imageButton);
        over.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });
    }
    public class MyFilter implements FileFilter{
        @Override
        public boolean accept(File pathname){
            String str=pathname.getName();
            if(str.endsWith(".mp4")||str.endsWith(".3gp")||str.endsWith(".MKV")||str.endsWith(".avi")){
                return true;
            }else return false;
        }
    }
}